<template>
  <div class="app-container">
    <!-- 顶部控制台 -->
    <div class="filter-container">
      <el-button type="primary" size="mini" icon="el-icon-edit" @click="handleOpenSavePanel">添加等级</el-button>
    </div>
    <!-- 页面数据列表 -->
    <el-table :data="responseData.tableList"
              border style="width: 100%;"
              v-loading="pageData.loading"
              element-loading-text="拼命加载中"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(0, 0, 0, 0.8)"
              :header-cell-style="{ background: '#545C64', color: '#ffffff' }"
              @selection-change="handleTableSelection">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column fixed="left" prop="expLevelId" label="规则ID" width="280" align="center"/>
      <el-table-column prop="expLevelName" label="等级名称" width="180" align="center"/>
      <el-table-column prop="expLevelNumber" label="等级编号" width="180" align="center"/>
      <el-table-column prop="expLevelLowCount" label="开始经验" width="180" align="center"/>
      <el-table-column prop="expLevelMaxCount" label="结束经验" width="180" align="center"/>
      <el-table-column prop="expLevelCount" label="升级经验" width="180" align="center"/>
      <el-table-column prop="expLevelStatus" label="审核状态" width="120" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.expLevelStatus === 0"><el-tag type="info">线下</el-tag></span>
          <span v-if="scope.row.expLevelStatus === 1"><el-tag type="success">线上</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="240" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleOpenEditPanel(scope)">编辑</el-button>
          <el-button v-if="scope.row.expLevelStatus === 0" size="mini" type="success" @click="handleUpdateDataStatus(scope, 1)">发布</el-button>
          <el-button v-if="scope.row.expLevelStatus === 1" size="mini" type="info" @click="handleUpdateDataStatus(scope, 0)">下架</el-button>
          <el-button type="danger" size="mini"  @click="handleDeleteData(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      layout="total, sizes, prev, pager, next, jumper"
      :current-page="form.pageNum"
      :page-size="form.pageSize"
      :total="form.total"
      :page-sizes="[12, 50, 100, 200]"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange">
    </el-pagination>
    <!-- 新增/修改隐藏域 -->
    <el-dialog title="等级配置" :visible.sync="pageData.savePanelStatus" size="small"
               :before-close="handleCloseSavePanel">
      <div>
        <el-form ref="ExpertSavePanelData" :model="form.expert" label-width="120px">
          <el-form-item label="等级名称">
            <el-input v-model="form.expert.expLevelName"></el-input>
          </el-form-item>
          <el-form-item label="等级编号">
            <el-input v-model="form.expert.expLevelNumber" disabled></el-input>
          </el-form-item>
          <el-form-item label="开始经验">
            <el-input v-model="form.expert.expLevelLowCount" disabled></el-input>
          </el-form-item>
          <el-form-item label="结束经验">
            <el-input v-model="form.expert.expLevelMaxCount" disabled></el-input>
          </el-form-item>
          <el-form-item label="升级经验">
            <el-input v-model="form.expert.expLevelCount" @change="handleExpLevelCountChange"></el-input>
          </el-form-item>
          <el-form-item label="审核状态">
            <el-radio-group v-model="form.expert.expLevelStatus">
              <el-radio :label="0">预设</el-radio>
              <el-radio :label="1">发布</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSaveButtonClick">保存等级</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  // 组件引入
  // 方法工具引入
  import { PermissionsToDetectFormUser } from '@/utils/PermissionsDetect';
  import { getPermission } from '@/utils/UserCookies';
  // 接口引入
  import { loadCmsExpertList,loadCmsExpertMaxData,saveCmsExpertData,updateCmsExpertData,batchReleaseCmsExpertData,deleteCmsExpertData } from "@/api/operation";
  export default {
    name: "expert",
    //import引入的组件需要注入到对象中才能使用
    components: {},
    data() {
      return {
        pageData:{
          loading: false,
          savePanelStatus: false,
          savePanelType: 0
        },// 页面配置数据
        form:{
          pageNum: 1,
          pageSize: 12,
          total: 0,
          selection: [],
          expert: {
            expLevelId: 0,
            expLevelName: "",
            expLevelNumber: 0,
            expLevelLowCount: 0,
            expLevelMaxCount: 0,
            expLevelCount: 0,
            expLevelStatus: 0
          }
        },// 页面表单
        formRules: {},// 表单验证
        responseData: {
          tableList: []
        },// 接口响应数据
      }
    },
    //监听属性 类似于data概念
    computed: {},
    //监控data中的数据变化
    watch: {},
    //方法集合
    methods:{
      // 事件函数
      handlePageInit(){
        this.pageData.loading = true;// 创建加载窗口
        this.loadCmsExpertList();// 读取路由角色列表
      },// 页面初始化事件
      handleCurrentChange(val){
        this.form.pageNum = val;// 更换页码
        this.handlePageInit();// 进行数据加载
      },// 页码切换事件
      handleSizeChange(val){
        this.form.pageSize = val;// 更换页面大小
        this.handlePageInit();// 进行数据加载
      },// 页面size切换事件
      handleTableSelection(val){
        let list = [];// 初始化多选数组
        for (let i=0; i<val.length; i++){
          list.push(val[i].expLevelId);
        }// 遍历多选的列表，将ID拼入数组中
        this.form.selection = list;// 进行多选值的切换
      },// 表格多选事件
      handleOpenSavePanel(){
        // 判断当前登录用户所拥有的的操作权限
        if (PermissionsToDetectFormUser(getPermission(), "250001")){
          this.loadCmsExpertMaxData();
        } else {
          // 当前权限不足，不执行操作，给与相应提示
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 打开新增面板
      handleOpenEditPanel(scope){
        // 判断当前登录用户所拥有的的操作权限
        if (PermissionsToDetectFormUser(getPermission(), '250003')){
          // 当前操作权限充足，进行相应操作
          // 初始化数据
          this.form.expert.expLevelId = scope.row.expLevelId;
          this.form.expert.expLevelName = scope.row.expLevelName;
          this.form.expert.expLevelNumber = scope.row.expLevelNumber;
          this.form.expert.expLevelLowCount = scope.row.expLevelLowCount;
          this.form.expert.expLevelMaxCount = scope.row.expLevelMaxCount;
          this.form.expert.expLevelCount = scope.row.expLevelCount;
          this.form.expert.expLevelStatus = scope.row.expLevelStatus;
          // 打开面板
          this.pageData.savePanelStatus = true;
          this.pageData.savePanelType = 2;
        } else {
          // 当前权限不足，不执行操作，给与相应提示
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 打开修改面板
      handleCloseSavePanel(){
        this.$confirm("确认关闭？").then(response => {
          // 初始化数据
          this.form.expert.expLevelId = 0;
          this.form.expert.expLevelName = 0;
          this.form.expert.expLevelNumber = 0;
          this.form.expert.expLevelLowCount = 0;
          this.form.expert.expLevelMaxCount = 0;
          this.form.expert.expLevelCount = 0;
          this.form.expert.expLevelStatus = 0;
          // 打开面板
          this.pageData.savePanelStatus = false;
          this.pageData.savePanelType = 0;
        }).catch(err => {
          console.log(err);
        });
      },// 关闭新增面板
      handleExpLevelCountChange(){
        // 通过修改升级经验值，自动获取当前最大经验值
        this.form.expert.expLevelMaxCount = parseInt(this.form.expert.expLevelLowCount) + parseInt(this.form.expert.expLevelCount);
      },// 检测升级经验是否合规
      handleSaveButtonClick(){
        // 判断当前登录用户所拥有的的操作权限
        if (PermissionsToDetectFormUser(getPermission(), "250001")){
          // 当前操作权限充足，进行敏感操作提示
          this.$confirm('此操作将会保存当前数据, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            // 同一操作，执行具体流程
            if (this.pageData.savePanelType === 1) {
              this.saveCmsExpertData();
            } else if (this.pageData.savePanelType === 2) {
              this.updateCmsExpertData();
            }
          }).catch(close => {
            console.log(close);
            this.$message.info('已取消操作');
          });
        } else {
          // 当前权限不足，不执行操作，给与相应提示
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 保存按钮点击事件
      handleUpdateDataStatus(scope, status){
        // 判断当前登录用户所拥有的的操作权限
        if (PermissionsToDetectFormUser(getPermission(), "250005")){
          // 当前操作权限充足，进行敏感操作提示
          this.$confirm('此操作将会修改当前发布状态, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            // 同一操作，执行具体流程
            let param = {
              expLevelId: scope.row.expLevelId,
              expLevelStatus: status
            };
            this.batchReleaseCmsExpertData(param);
          }).catch(close => {
            console.log(close);
            this.$message.info('已取消操作');
          });
        } else {
          // 当前权限不足，不执行操作，给与相应提示
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 更改发布状态
      handleDeleteData(scope){
        // 判断当前登录用户所拥有的的操作权限
        if (PermissionsToDetectFormUser(getPermission(), "250002")){
          // 当前操作权限充足，进行敏感操作提示
          this.$confirm('此操作将会删除当前数据, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            // 同一操作，执行具体流程
            let param = {
              expLevelId: scope.row.expLevelId
            };
            this.deleteCmsExpertData(param);
          }).catch(close => {
            console.log(close);
            this.$message.info('已取消操作');
          });
        } else {
          // 当前权限不足，不执行操作，给与相应提示
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 删除数据按钮
      // 接口
      loadCmsExpertList(){
        loadCmsExpertList(this.form).then(response => {
          if (response.code === "200"){
            this.pageData.loading = false;
            this.responseData.tableList = response.data.list;
            this.form.total = response.data.total;
          } else {
            this.$message.warning(response.message);
          }
        }).catch( error =>{
          console.log(error);
          this.$message.error(error);
        })
      },// 获取等级配置列表数据
      loadCmsExpertMaxData(){
        loadCmsExpertMaxData().then(response => {
          if (response.code === "200"){
            // 初始化数据
            this.form.expert.expLevelId = response.data.expLevelId;
            this.form.expert.expLevelName = response.data.expLevelName;
            this.form.expert.expLevelNumber = response.data.expLevelNumber+1;
            this.form.expert.expLevelLowCount = response.data.expLevelLowCount+1;
            this.form.expert.expLevelMaxCount = response.data.expLevelMaxCount + response.data.expLevelCount;
            this.form.expert.expLevelCount = response.data.expLevelCount;
            this.form.expert.expLevelStatus = response.data.expLevelStatus;
            // 打开面板
            this.pageData.savePanelStatus = true;
            this.pageData.savePanelType = 1;
          } else {
            this.$message.warning(response.message);
          }
        }).catch(error => {
          console.log(error);
          this.$message.error(error);
        });
      },// 获取等级配置最大值
      saveCmsExpertData(){
        saveCmsExpertData(this.form.expert).then(response => {
          if (response.code === '200'){
            this.$message.success('添加等级成功!');
            // 初始化数据
            this.form.expert.expLevelId = 0;
            this.form.expert.expLevelName = 0;
            this.form.expert.expLevelNumber = 0;
            this.form.expert.expLevelLowCount = 0;
            this.form.expert.expLevelMaxCount = 0;
            this.form.expert.expLevelCount = 0;
            this.form.expert.expLevelStatus = 0;
            // 打开面板
            this.pageData.savePanelStatus = false;
            this.pageData.savePanelType = 0;
            // 刷新数据
            this.handlePageInit();
          } else {
            this.$message.warning(response.message);
          }
        }).catch(error => {
          console.log(error);
          this.$message.error(error);
        })
      },// 保存等级数据
      updateCmsExpertData(){
        updateCmsExpertData(this.form.expert).then(response => {
          if (response.code === '200'){
            this.$message.success('添加等级成功!');
            // 初始化数据
            this.form.expert.expLevelId = 0;
            this.form.expert.expLevelName = 0;
            this.form.expert.expLevelNumber = 0;
            this.form.expert.expLevelLowCount = 0;
            this.form.expert.expLevelMaxCount = 0;
            this.form.expert.expLevelCount = 0;
            this.form.expert.expLevelStatus = 0;
            // 打开面板
            this.pageData.savePanelStatus = false;
            this.pageData.savePanelType = 0;
            // 刷新数据
            this.handlePageInit();
          } else {
            this.$message.warning(response.message);
          }
        }).catch(error => {
          console.log(error);
          this.$message.error(error);
        })
      },// 更新等级配置数据
      batchReleaseCmsExpertData(param){
        batchReleaseCmsExpertData(param).then(response => {
          if (response.code === '200'){
            this.$message.success('更新成功!');
            // 刷新数据
            this.handlePageInit();
          } else {
            this.$message.warning(response.message);
          }
        }).catch(error => {
          console.log(error);
          this.$message.error(error);
        })
      },// 更改等级配置数据的发布状态
      deleteCmsExpertData(param){
        deleteCmsExpertData(param).then(response => {
          if (response.code === '200'){
            this.$message.success('删除成功!');
            // 刷新数据
            this.handlePageInit();
          } else {
            this.$message.warning(response.message);
          }
        }).catch(error => {
          console.log(error);
          this.$message.error(error);
        })
      },// 更新等级配置数据
    },
    //生命周期 - 创建完成（可以访问当前this实例）
    created: function () {
      let _this = this;
      setTimeout(function () {
        _this.handlePageInit();
      }, 800);
    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {},
    beforeCreate() {}, //生命周期 - 创建之前
    beforeMount() {}, //生命周期 - 挂载之前
    beforeUpdate() {}, //生命周期 - 更新之前
    updated() {}, //生命周期 - 更新之后
    beforeDestroy() {}, //生命周期 - 销毁之前
    destroyed() {}, //生命周期 - 销毁完成
    activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
  };
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>
